﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
namespace BanHangCshap.Class
{
    class NguoiDung:Database
    {
        #region ***** Fields & Properties *****
        private string _tendn;
        public string tendn
        {
            get
            {
                return _tendn;
            }
            set
            {
                _tendn = value;
            }
        }
        private string _matkhau;
        public string matkhau
        {
            get
            {
                return _matkhau;
            }
            set
            {
                _matkhau = value;
            }
        }
        private string _hoten;
        public string hoten
        {
            get
            {
                return _hoten;
            }
            set
            {
                _hoten = value;
            }
        }
        private int _manhomnd;
        public int manhomnd
        {
            get
            {
                return _manhomnd;
            }
            set
            {
                _manhomnd = value;
            }
        }
        #endregion

        #region ***** Init Methods *****
        public NguoiDung()
        {
        }
        public NguoiDung(string tendn)
        {
            string sql = "select * from NguoiDung where tendn = '" + tendn + "'";
            foreach (DataRow dr in getDataTable(sql).Rows)
            {
                this.tendn = tendn;
                this.hoten = dr["hoten"].ToString().Trim();
                this.matkhau = dr["matkhau"].ToString().Trim();
                this.manhomnd = int.Parse(dr["manhomnd"].ToString());
            }
        }
        public NguoiDung(string tendn, string matkhau, string hoten, int manhomnd)
        {
            this.tendn = tendn;
            this.matkhau = matkhau;
            this.hoten = hoten;
            this.manhomnd = manhomnd;
        }
        #endregion
        public Boolean dangNhap()
        {
            string sql = "select * from NguoiDung where tendn = '" + this.tendn + "' and matkhau = '" + this.matkhau + "'";
            return isExist(sql);
        }
        public DataTable layThongTinNguoiDung(string tendn, string matkhau)
        {
            string sql = "select * from NguoiDung  where tendn = '" + tendn + "' and matkhau = '" + matkhau + "'";

            return getDataTable(sql);
        }
        //public DataTable layDSNguoiDung()
        //{
        //    string sql = "select tendn, hoten,tennhomnd from NguoiDung a, NhomNguoiDung b where a.manhomnd = b.manhomnd ";

        //    return getDataTable(sql);
        //}
        public Boolean isExistNguoiDung(string tendn)
        {
            string sql = "select tendn from NguoiDung where tendn = '" + tendn + "'";
            return isExist(sql);
        }
        public Boolean themNguoiDung()
        {
            string sql = "insert into NguoiDung(tendn,matkhau,hoten, manhomnd) values ('" + tendn + "', '" + matkhau + "', N'" + hoten + "'," + manhomnd + ")";
            return executeQuery(sql);
        }
        public Boolean xoaNguoiDung(string tendn)
        {
            string sql = "delete NguoiDung where tendn = '" + tendn + "' ";
            return executeQuery(sql);
        }
        public Boolean suaNguoiDung(string tendncu)
        {
            string sql = "update NguoiDung set tendn = '" + tendn + "', hoten = N'" + hoten + "',  matkhau = '" + matkhau + "' where tendn = '" + tendncu + "' ";
            return executeQuery(sql);
        }
        //public DataTable layDSNguoiDung()
        //{
        //    string sql = "select nhomnd.manhomnd,tennhomnd, hoten, tendn,matkhau ";
        //    sql += "from (select  manhomnd, tennhomnd from NhomNguoiDung) nhomnd ";
        //    sql += "left join ";
        //    sql += "(select  hoten, tendn,matkhau, manhomnd from NguoiDung ) nd ";
        //    sql += "on nhomnd.manhomnd = nd.manhomnd ";
        //    return getDataTable(sql);
        //}
        public DataTable layDSNguoiDung()
        {
            string sql = "select * from NguoiDung ";
          
            return getDataTable(sql);
        }
        public DataTable layDSQuyenNguoiDung(int mand)
        {
            string sql = "select macn from NguoiDung a, NhomNguoiDung b, ChucNangNhomND c  where tendn = '" + tendn + "'  and a.manhomnd = b.manhomnd and a.manhomnd = c.manhomnd";
            return getDataTable(sql);
        }
        
    }
}
